/**
 * API 接口封装模块
 * 提供统一的项目API调用接口（TypeScript版本）
 */

import http from './utils/http'
import type { Movie } from './utils/http'

/**
 * 电影相关API接口定义
 */
export interface ApiResponse<T = any> {
  data: T
  total?: number
  start?: number
  count?: number
}

/**
 * 电影相关API
 */
export const movieAPI = {
  /**
   * 获取热门电影列表
   * @param params 查询参数
   * @returns 返回电影列表数据
   */
  async getHotMovies(params: {
    category?: string
    type?: string
    limit?: number
    start?: number
  } = {}): Promise<ApiResponse<Movie[]>> {
    const {
      category = '热门',
      type = '全部',
      limit = 20,
      start = 0
    } = params
    
    const response = await http.get('/douban/categories', {
      params: {
        kind: 'movie',
        category,
        type,
        limit,
        start
      }
    })
    return response
  },

  /**
   * 搜索电影
   * @param query 搜索关键词
   * @param options 搜索选项
   * @returns 返回搜索结果
   */
  async searchMovies(
    query: string,
    options: {
      limit?: number
      start?: number
    } = {}
  ): Promise<ApiResponse<Movie[]>> {
    const { limit = 20, start = 0 } = options
    
    const response = await http.get('/douban/search', {
      params: {
        q: query,
        limit,
        start
      }
    })
    return response
  }
}

/**
 * 剧集相关API
 */
export const tvAPI = {
  /**
   * 获取热门剧集列表
   * @param params 查询参数
   * @returns 返回剧集列表数据
   */
  async getHotTVShows(params: {
    category?: string
    type?: string
    limit?: number
    start?: number
  } = {}): Promise<ApiResponse<Movie[]>> {
    const {
      category = 'tv',
      type = 'tv',
      limit = 20,
      start = 0
    } = params
    
    const response = await http.get('/douban/categories', {
      params: {
        kind: 'tv',
        category,
        type,
        limit,
        start
      }
    })
    return response
  }
}

/**
 * 综艺相关API
 */
export const varietyAPI = {
  /**
   * 获取热门综艺列表
   * @param params 查询参数
   * @returns 返回综艺列表数据
   */
  async getHotVarietyShows(params: {
    category?: string
    type?: string
    limit?: number
    start?: number
  } = {}): Promise<ApiResponse<Movie[]>> {
    const {
      category = 'show',
      type = 'show',
      limit = 20,
      start = 0
    } = params
    
    const response = await http.get('/douban/categories', {
      params: {
        kind: 'tv',
        category,
        type,
        limit,
        start
      }
    })
    return response
  }
}

/**
 * 通用API方法
 */
export const commonAPI = {
  /**
   * 获取视频详情
   * @param id 视频ID
   * @returns 返回视频详情数据
   */
  async getVideoDetail(id: string): Promise<Movie> {
    const response = await http.get(`/douban/subject/${id}`)
    return response
  },

  /**
   * 获取分类列表
   * @param kind 类型（movie/tv）
   * @param params 其他参数
   * @returns 返回分类数据
   */
  async getCategories(
    kind: string,
    params: Record<string, any> = {}
  ): Promise<ApiResponse<Movie[]>> {
    const response = await http.get('/douban/categories', {
      params: {
        kind,
        ...params
      }
    })
    return response
  }
}